package utils;

public class EditorContext {
    private StringBuilder content;
    private String currentFilePath;

    public enum EditorState {
        IDLE, EDITING
    }

    public enum SavingState {
        ToSave, Saved
    }

    private EditorState state = EditorState.IDLE;
    private SavingState savingState = SavingState.Saved;

    // 判断是否是 Markdown 标题
    private String extractPlainText(String line) {
        String pattern = "^(#+|\\*|\\-|\\+|\\d+\\.) (.+)";
        return line.replaceFirst(pattern, "$2");
    }

    public EditorContext() {
        this.content = new StringBuilder();
    }

    public EditorContext(StringBuilder content) {
        this.content = content;
    }

    public EditorContext(EditorContext context) {
        this.content = new StringBuilder(context.getContent());
        this.currentFilePath = context.getCurrentFilePath();
        this.state = context.getState();
    }

    public String getContent() {
        return content.toString();
    }

    public void setContent(String content) {
        this.content.setLength(0); // clear the current content
        this.content.append(content);
    }

    public String getCurrentFilePath() {
        return currentFilePath;
    }

    public void setCurrentFilePath(String currentFilePath) {
        this.currentFilePath = currentFilePath;
    }

    public EditorState getState() {
        return state;
    }

    public void setState(EditorState state) {
        this.state = state;
    }

    public SavingState getSavingState() {
        return savingState;
    }

    public void setSavingState(SavingState savingState) {
        this.savingState = savingState;
    }

    public boolean insert(int lineNumber, String text) {
        String[] lines = content.toString().split("\n", -1); // -1 keeps trailing empty lines

        // 如果行号为 -1 或者要插入到最后 or行号不在有效范围内
        if (lineNumber == -1 || lineNumber >= lines.length) {
            content.append(text).append("\n");
            return true;
        }

        // 如果行号不在有效范围内
        if (lineNumber <= 0) {
            System.out.println("Invalid line number. The document has " + lines.length + " lines.");
            return false;
        }

        StringBuilder newContent = new StringBuilder();

        for (int i = 0; i < lines.length; i++) {
            // 插入新的文本到指定行
            if (i == lineNumber - 1) {
                newContent.append(text).append("\n");
                newContent.append(lines[i]).append("\n");
            } else if (i != lines.length - 1)
                newContent.append(lines[i]).append("\n");
            else newContent.append(lines[i]);
        }
        content.setLength(0);  // 清空原内容
        content.append(newContent);  // 设置新的内容
        return true;
    }

    public boolean delete(int lineNumber, String text) {
        String[] lines = content.toString().split("\n", -1); // -1 keeps trailing empty lines
        StringBuilder newContent = new StringBuilder();
        if (lineNumber == -1) {
            boolean flag = true;
            for (int i = 0; i < lines.length; i++) {
                String compare_text = extractPlainText(lines[i]);
                if (compare_text.equals(text)) {
                    flag = false;
                    continue;
                }
                if (i == lines.length - 1)
                    newContent.append(lines[i]);
                else
                    newContent.append(lines[i]).append("\n");

            }
            if (flag) {
                System.out.println("Invalid text. Please input the extract same deleting text.\n Your file content: ");
                System.out.print(content);
                return false;
            }
        } else {
            // 如果行号不在有效范围内
            if (lineNumber <= 0 || lineNumber > lines.length) {
                System.out.println("Invalid line number. The document has " + lines.length + " lines.");
                return false;
            } else {
                // 删除指定行
                for (int i = 0; i < lines.length; i++) {
                    if (i != lineNumber - 1) {
                        if (i == lines.length - 1)
                            newContent.append(lines[i]);
                        else
                            newContent.append(lines[i]).append("\n");
                    }
                }
            }
        }
        content.setLength(0);
        content.append(newContent);
        System.out.print(newContent);
        return true;
    }

}
